﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Operations_Views_AdvancedSearchView : BaseHostedView
{
    #region Fields

    private ISearchService searchService;

    #endregion

    #region Properties

    /// <summary>
    /// Sets initialized search service instance. This property is utilized by 
    /// the IoC container.
    /// </summary>
    public ISearchService SearchService
    {
        set
        {
            this.searchService = value;
        }
    }

    #endregion

    #region Event handlers

    protected void SearchLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (!String.IsNullOrEmpty(this.queryTextBox.Text))
            {
                base.Navigate(DocumentsViews.SearchResultsView, DocumentsViews.AdvancedSearchView, 
                              this.searchService.ExecuteTextQuery(this.queryTextBox.Text));
            }
            else if (!String.IsNullOrEmpty(this.fileUpload.UploadedFileName))
            {
                base.Navigate(DocumentsViews.SearchResultsView, DocumentsViews.AdvancedSearchView,
                              this.searchService.ExecuteSpokenQuery(this.fileUpload.UploadedFile.Data, -1));
            }
            else
                throw new InvalidOperationException("Specify either textual or audio query");
        }
        catch (Exception ex)
        {
            base.Navigate(DocumentsViews.ErrorView, DocumentsViews.AdvancedSearchView, ex);
        }
        finally
        {
            fileUpload.Clean();
        }
    }

    #endregion    
}
